---
title: "Cleaning Eurobarometer"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load Eurobarometer: round 30
  eurobarometer_30_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer30/eurobarometer30.dta")

# load Eurobarometer: round 37
  eurobarometer_37_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer37/eurobarometer37.dta")
  
# load Eurobarometer: round 39
  eurobarometer_39_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer39/eurobarometer39.dta")
  
# load Eurobarometer: round 42
 eurobarometer_42_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer42/eurobarometer42.dta")
 
# load Eurobarometer: round 47.2
  eurobarometer_47.2_raw <- 
    import("../raw-data/y-multi-eurobarometer/eurobarometer47.2/eurobarometer47_2.dta")
 
# load Eurobarometer: round 48
  eurobarometer_48_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer48/eurobarometer48.dta")
 
# load Eurobarometer: round 53
  eurobarometer_53_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer53/eurobarometer53.dta")
  
# load Eurobarometer: round 69.1
  eurobarometer_69.1_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer69.1/eurobarometer69_1.dta")
  
# load Eurobarometer: round 83.4
  eurobarometer_83.4_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer83.4/eurobarometer83_4.dta")

# load Eurobarometer: round 91.4
  eurobarometer_91.4_raw <- 
    read_dta("../raw-data/y-multi-eurobarometer/eurobarometer91.4/eurobarometer91_4.dta")
```

# Clean Round 30

```{r}
# dates
  extra_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "France", "22 Oct 88", "10 Nov 88",
      "Belgium", "18 Oct 88", "06 Nov 88",
      "Netherlands", "22 Oct 88", "06 Nov 88",
      "Germany", "17 Oct 88", "09 Nov 88", # west germany is germany in the common code
      "Italy", "26 Oct 88", "10 Nov 88",
      "Luxembourg", "20 Oct 88", "21 Nov 88",
      "Denmark", "31 Oct 88", "19 Nov 88",
      "Ireland", "18 Oct 88", "10 Nov 88",
      "UK", "20 Oct 88", "15 Nov 88", # includes northern ireland
      "Greece", "17 Oct 88", "07 Nov 88",
      "Spain", "17 Oct 88", "04 Nov 88",
      "Portugal", "19 Oct 88", "15 Nov 88") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%d %b %y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%d %b %y"))

# clean
  clean_eurobarometer_30 <- 
    eurobarometer_30_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 30", #  (Oct-Nov 1988)
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3POLMQJ",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(v5),
            "1" = "France",
            "2" = "Belgium",
            "3" = "Netherlands",
            "4" = "Germany",
            "5" = "Italy",
            "6" = "Luxembourg",
            "7" = "Denmark",
            "8" = "Ireland",
            "9" = "United Kingdom",
            "10" = "Greece",
            "11"= "Spain",
            "12" = "Portugal"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = NA) %>% 
        left_join(extra_dates, by = "resp_country_common") %>% 
        mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(v715),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Jewish",
            "5" = "Muslim",
            "6" = "Buddhist",
            "7" = "Hindu",
            "8" = "Other religion",
            "9" = NA_character_,
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(v715),
      
      # respondent's age (character vector; exact age.Note: 15 = 15 YEARS OR LESS.73 (GB-GBN: 73 years and more) 99 99 YEARS OR MORE)
        v690 = as.character(v690),
        resp_age = 
          case_when(
            v690 == "15" ~ "15-",
            isocntry == "GB-GBN" & v690 == "73" ~ "73+",
            isocntry != "GB-GBN" & v690 == "99" ~ "99+",
            v690 != "99" & v690 != "15" ~ as.character(v690)),
        
      # respondent's education level
        resp_education_original = 
          dplyr::recode(
            as.character(v685),
            "1" = "1. Stopped education by 14 years old [Primary]",
            "2" = "2. Stopped education at 15 years old [Primary]",
            "3" = "3. Stopped education at 16 years old [Primary]",
            "4" = "4. Stopped education at 17 years old [Primary]",
            "5" = "5. Stopped education at 18 years old [Primary]",
            "6" = "6. Stopped education at 19 years old [Primary]",
            "7" = "7. Stopped education at 20 years old [Primary]",
            "8" = "8. Stopped education at 21 years old [College]",
            "9" = "9. Stopped education at 22 years or older [College]",
            "10" = "10. Still studying [Primary]"),        
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
      # raw records 0 as NA, 1 = man, 2 = woman
        resp_female = 
          case_when(
            v689 == 2 ~ 1,
            v689 == 1 ~ 0,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
      # raw eurob records 0 DK/NA
                         #1 RURAL AREA OR VILLAGE (I classify as rural)
                         #2 SMALL OR MIDDLE SIZE TOWN (I classify as urban)
                         #3 BIG TOWN (I classify as urban)
        resp_rural = 
          dplyr::recode(
          as.character(v714),
            "0" = NA_real_,
            "1" = 1,
            "2" = 0,
            "3" = 0),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: v238 [Q.223/225]; QTEXT: Some people are disturbed by the opinions, customs and way of life of people different from themselves. And do you find disturbing the presence of people of another religion?; ROPTIONS: 1 = Disturbing [=1] + 2 = Not disturbing [=0]; TARGET: Different religion; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(v238),

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v238 %in% c("1") ~ 1,
            v238 %in% c("2") ~ 0,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################

      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: v293 [Q.236/243/241_C]; QTEXT: I am going to read you out opinions. For each opinion I read out, please tell me to which, if any, kinds of people it applies. Marrying into one of these groups always ends badly. People of another religion.; ROPTIONS: 0 = Not mentioned [=0] + 1 = Mentioned [=1]; TARGET: Different religion; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_2_original = as.character(v293),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            v293 %in% c("0") ~ 0,
            v293 %in% c("1") ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: v300 [Q.236/243/242_C]; QTEXT: I am going to read out to you opinions. For each opinion I read out, please tell me to which, if any, kind of people it applies. To have them as neighbors creates problems. People of another religion.; ROPTIONS: 0 = Not mentioned [=0] + 1 = Mentioned [=1]; TARGET: Different religion; TYPE: Distance, neighbor",
      
      # original response (as character vector)
        resp_soc_dist_3_original = as.character(v300),

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            v300 %in% c("0") ~ 0,
            v300 %in% c("1") ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q561 [v717]; QTEXT: Whether you do or you don't follow religious practices, would you say that you are...; ROPTIONS: 1 = A religious person + 2 = A non-religious person + 3 = A confirmed atheist",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v717),
            `NA` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = 
          case_when(
            v717 == 1 ~ 1,
            v717 %in% c(2:3) ~ 0,
            TRUE ~ NA_real_)
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Round 37

* Small number of missing individual dates, imputed using range (e.g. `range(clean_eurobarometer_37$resp_interview_date[clean_eurobarometer_37$resp_country_common=="Norway"], na.rm = T)`)

```{r}
# declare dates
  dates_r37 <- 
      tribble(
        ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
        "Belgium", "1992-03-23", "1992-04-04", 
        "Germany", "1992-03-01", "1992-04-03",
        "Norway", "1992-03-21", "1992-04-19") %>% 
      mutate(
        resp_interview_start_date = as.Date(resp_interview_start_date),
        resp_interview_end_date = as.Date(resp_interview_end_date))

# clean
  clean_eurobarometer_37 <- 
    eurobarometer_37_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 37.0",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3wZktug",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(v5),
            "1" = "France",
            "2" = "Belgium",
            "3" = "Netherlands",
            "4" = "Germany", #"Germany - West" becomes Germany; GDR dissolved Oct 3, 1990
            "5" = "Italy",
            "6" = "Luxembourg",
            "7" = "Denmark",
            "8" = "Ireland",
            "9" = "United Kingdom",
            "10" = "Greece",
            "11" = "Spain",
            "12" = "Portugal",
            "13" = "Germany", #"Germany - East" becomes Germany; GDR dissolved Oct 3, 1990
            "14" = "Norway"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = make_date(1992, v652, v651)) %>% 
        left_join(
          dates_r37, by = "resp_country_common") %>% 
        mutate(
      
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
    
    # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(v646),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Jewish",
            "5" = "Muslim",
            "6" = "Buddhist",
            "7" = "Hindu",
            "8" = "Other religion",
            "9" = NA_character_,
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(v646),
    
      # respondent's age (character vector; exact age.Note: 15 = 15 YEARS OR LESS.73 (GB-GBN: 73 years and more) 99 99 YEARS OR MORE)
        resp_age = as.character(v616),
           
      # respondent's education level
        resp_education_original = dplyr::recode(
            as.character(v612),
            "1" = "1. Stopped education by 14 years old [Primary]",
            "2" = "2. Stopped education at 15 years old [Primary]",
            "3" = "3. Stopped education at 16 years old [Primary]",
            "4" = "4. Stopped education at 17 years old [Primary]",
            "5" = "5. Stopped education at 18 years old [Primary]",
            "6" = "6. Stopped education at 19 years old [Primary]",
            "7" = "7. Stopped education at 20 years old [Primary]",
            "8" = "8. Stopped education at 21 years old [College]",
            "9" = "9. Stopped education at 22 years or older [College]",
            "10" = "10. Still studying [Primary]"), 
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
    #raw records 0 as NA, 1 = man, 2 = woman
        resp_female = as.numeric((v615 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
    #raw eurob records  0 DK/NA
                       #1 RURAL AREA OR VILLAGE (I classify as rural)
                       #2 SMALL OR MIDDLE SIZE TOWN (I classify as urban)
                       #3 BIG TOWN (I classify as urban)
        resp_rural =
          dplyr::recode(
            as.character(v645),
            "0" = NA_real_,
            "1" = 1,
            "2" = 0,
            "3" = 0),
        
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: v448 [Q.73C]; QTEXT: Some people are disturbed by the opinions, customs and way of life of people different from themselves. And do you find the presence of people of another religion disturbing?; ROPTIONS: 1 = Disturbing [=1] + 2 = Not Disturbing [=0]; TARGET: Different religion; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(v448),

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v448 %in% c("1") ~ 1, 
            v448 %in% c("2") ~ 0, 
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: D28 [v648]; QTEXT: Whether you do or you don't follow religious practices, would you say that you are...; ROPTIONS: 1 = Religious + 2 = Not religious + 3 = An agnostic + 4 = An atheist",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v648),
            `0` = NA_real_,
            `5` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = 
          case_when(
            v648 == 1 ~ 1,
            v648 %in% c(2:4) ~ 0,
            TRUE ~ NA_real_)
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Round 39

```{r}
# declare dates
  dates_r39 <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "Norway", "1993-03-21", "1993-04-26", # a couple missing; imputed from max/min
      "Finland", "1993-03-28", "1993-04-30", # all missing; range from technical documentation
      ) %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date),
      resp_interview_end_date = as.Date(resp_interview_end_date))

# clean
  clean_eurobarometer_39 <- 
    eurobarometer_39_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 39.0",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3a30naM",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(v5),
            "1" = "France",
            "2" = "Belgium",
            "3" = "Netherlands",
            "4" = "Germany", #"Germany - West" becomes Germany; GDR dissolved Oct 3, 1990
            "5" = "Italy",
            "6" = "Luxembourg",
            "7" = "Denmark",
            "8" = "Ireland",
            "9" = "United Kingdom",
            "10" = "Greece",
            "11" = "Spain",
            "12" = "Portugal",
            "13" = "Germany", #"Germany - East" becomes Germany; GDR dissolved Oct 3, 1990
            "14" = "Norway",
            "15" = "Finland"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = make_date(1993, v732, v731)) %>% 
        left_join(
          dates_r39, by = "resp_country_common") %>% 
        mutate(
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(v725),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Jewish",
            "5" = "Muslim",
            "6" = "Buddhist",
            "7" = "Hindu",
            "8" = "Other religion",
            "9" = NA_character_,
            .default = NA_character_),
    
      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(v725),   
    
      # respondent's age (character vector)
        resp_age = as.character(v687),
           
      # respondent's education level
        resp_education_original = # quite a lot of missingness but evenly spread across countries (and far less missingness than v742, "general edu")
          case_when(
            v683 == 0 ~ NA_character_,
            v683 <= 14 & v683 > 0 ~ '1. Stopped education by 14 years old [Primary]',
            v683 == 15 ~ '2. Stopped education at 15 years old [Primary]',
            v683 == 16 ~ '3. Stopped education at 16 years old [Primary]',
            v683 == 17 ~ '4. Stopped education at 17 years old [Primary]',
            v683 == 18 ~ '5. Stopped education at 18 years old [Primary]',
            v683 == 19 ~ '6. Stopped education at 19 years old [Primary]',
            v683 == 20 ~ '7. Stopped education at 20 years old [Primary]',
            v683 == 21 ~ '8. Stopped education at 21 years old [College]',
            v683 >= 22 & v683 < 97 ~ '9. Stopped education at 22 years and older [College]',
            v683 >= 98 ~ '10. Still studying [Primary]'),
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
    #raw records 0 as NA, 1 = man, 2 = woman
        resp_female = as.numeric((v686 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
    #raw eurob records  0 DK/NA
                       #1 RURAL AREA OR VILLAGE (I classify as rural)
                       #2 SMALL OR MIDDLE SIZE TOWN (I classify as urban)
                       #3 BIG TOWN (I classify as urban)
                       #4 DK
        resp_rural =
          dplyr::recode(
            as.character(v724),
            "0" = NA_real_,
            "1" = 1,
            "2" = 0,
            "3" = 0,
            "4" = NA_real_),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: v163 [Q.36]; QTEXT: Some people are disturbed by the opinions, customs and way of life of people different from themselves. And do you find the presence of people of another religion disturbing?; ROPTIONS: 1 = Disturbing [=1] + 2 = Not Disturbing [=0]; TARGET: Different religion; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(v163),

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v163 %in% c("1") ~ 1,
            v163 %in% c("2") ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: D28 [v727]; QTEXT: Whether you do or you don't follow religious practices, would you say that you are...; ROPTIONS: 1 = Religious + 2 = Not religious + 3 = An agnostic + 4 = An atheist",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v727),
            `0` = NA_real_,
            `5` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = 
          case_when(
            v727 == 1 ~ 1,
            v727 %in% c(2:4) ~ 0,
            TRUE ~ NA_real_)
    
    ) %>% 
    select(starts_with("resp_"))
```

# Clean Eurobarometer 42

```{r}
# clean
  clean_eurobarometer_42 <- 
    eurobarometer_42_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 42",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3z6D1eI",

      # survey mode (in-person/phone/internet)
        resp_survey_mode =  case_when(
            isocntry != "SE" ~ "in-person",
            isocntry == "SE" ~ "phone",
          ),  

      # country (character vector; list of countries as written in original source)
        resp_country_original = dplyr::recode(
            as.character(isocntry),
            "AT" = "Austria",
            "BE" = "Belgium",
            "DE-E" = "Germany", #"Germany - West/East" becomes Germany; GDR dissolved Oct 3, 1990
            "DE-W" = "Germany", #"Germany - West/East" becomes Germany; GDR dissolved Oct 3, 1990
            "DK" = "Denmark",
            "ES" = "Spain",
            "FI" = "Finland",
            "FR" = "France",
            "GB-GBN" = "United Kingdom",
            "GB-NIR" = "United Kingdom",
            "GR" = "Greece",
            "IE" = "Ireland",
            "IT" = "Italy",
            "LU" = "Luxembourg",
            "NO" = "Norway",
            "NL" = "Netherlands",
            "PT" = "Portugal",
            "SE" = "Sweden",
            .default = NA_character_),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(dplyr::recode(
            as.character(v524),
            "1" = "1994-11-28",
            "2" = "1994-11-29",
            "3" = "1994-11-30",
            "4" = "1994-12-01",
            "5" = "1994-12-02",
            "6" = "1994-12-03",
            "7" = "1994-12-04",
            "8" = "1994-12-05",
            "9" = "1994-12-06",
            "10" = "1994-12-07",
            "11" = "1994-12-08",
            "12" = "1994-12-09",
            "13" = "1994-12-10",
            "14" = "1994-12-11",
            "15" = "1994-12-12",
            "16" = "1994-12-13",
            "17" = "1994-12-14",
            "18" = "1994-12-15",
            "19" = "1994-12-16",
            "20" = "1994-12-17",
            "21" = "1994-12-18",
            "22" = "1994-12-19",
            "23" = "1994-12-20",
            "24" = "1994-12-21",
            "25" = "1994-12-22",
            "26" = "1994-12-23",
            "27" = "1994-12-24",
            "28" = "1994-12-25",
            "29" = "1994-12-26",
            "30" = "1994-12-27",
            "31" = "1994-12-28",
            "32" = "1994-12-29",
            "33" = "1994-12-30",
            "34" = "1994-12-31",
            .default = NA_character_)),
    
        resp_interview_start_date = 
          case_when(
            resp_country_original == "Finland" ~ as.Date("1994-11-14"), # individual dates are missing but ranges available in documentation
            resp_country_original == "Austria" ~ as.Date("1994-12-01"),
            resp_country_original == "Sweden" ~ as.Date("1994-12-01"),
            TRUE ~ NA_Date_),
        resp_interview_end_date = 
          case_when(
            resp_country_original == "Finland" ~ as.Date("1994-11-27"),
            resp_country_original == "Austria" ~ as.Date("1994-12-13"),
            resp_country_original == "Sweden" ~ as.Date("1994-12-13"),
            TRUE ~ NA_Date_),    
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(v496),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Jewish",
            "5" = "Muslim",
            "6" = "Buddhist",
            "7" = "Hindu",
            "8" = "Other religion",
            "9" = NA_character_,
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(v496),
    
      # respondent's age
        resp_age = as.character(v470),
    
      # respondent's education level
        v500 = as.character(v500),
        v502 = as.character(v502),
        v517 = as.character(v517),
        v466 = as.character(v466),
        resp_education_original =
              case_when(
              v500 == "1" & isocntry == "NO" ~ 
                "1. Primary school 6 years or less [Primary]",
              v500 == "2" & isocntry == "NO"  ~ 
                "2. Junior secondary school 7-9 years [Primary]",
              v500 == "3" & isocntry == "NO"  ~ 
                "3. High school/grammar school [Primary]",
              v500 == "4" & isocntry == "NO" ~ 
                "4. University, college [College]",
              v502 == "1" & isocntry == "SE" ~ 
                "1. Elementary school [Primary]",
              v502 == "2" & isocntry == "SE"  ~ 
                "2. Continuation school [Primary]",
              v502 == "3" & isocntry == "SE"  ~ 
                "3. Upper secondary school [Primary]",
              v502 == "4" & isocntry == "SE" ~ 
                "4. College / University [College]",
              v517 == "1" & isocntry == "AT" ~ 
                "1. Primary school [Primary]",
              v517 == "2" & isocntry == "AT"  ~ 
                "2. Secondary school [Primary]",
              v517 == "3" & isocntry == "AT"  ~ 
                "3. University [College]",
              v466 == "1" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "1. Stopped by 14 years old [Primary]",
              v466 == "2" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "2. Stopped at 15 years old [Primary]",
              v466 == "3" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "3. Stopped at 16 years old [Primary]",
              v466 == "4" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "4. Stopped at 17 years old [Primary]",
              v466 == "5" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "5. Stopped at 18 years old [Primary]",
              v466 == "6" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "6. Stopped at 19 years old [Primary]",
              v466 == "7" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "7. Stopped at 20 years old [Primary]",
              v466 == "8" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "8. Stopped at 21 years old [College]",
              v466 == "9" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "9. Stopped at 22 years and older [College]",
              v466 == "10" & isocntry != "NO" & isocntry != "SE" & isocntry != "AT" ~ "10. Still studying [Primary]",
              TRUE ~ NA_character_),
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = as.numeric((v469 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        v5 = as.character(v5),
        v529 = as.character(v529),
        resp_rural = case_when(
          v5 == '1' & v529 == '1' ~ 1,
          v5 == '1' & v529  == '2' ~ 1,
          v5 == '1' & v529  == '3' ~ 0,
          v5 == '1' & v529  == '4' ~ 0,
          v5 == '1' & v529  == '5' ~ 0,
          
          v5 == '2' & v529  == '1' ~ 0,
          v5 == '2' & v529  == '2' ~ 0,
          v5 == '2' & v529  == '3' ~ 1,
          v5 == '2' & v529  == '4' ~ 1,
          
          v5 == '3' & v529  == '1' ~ 1,
          v5 == '3' & v529  == '2' ~ 1,
          v5 == '3' & v529  == '3' ~ 0,
          v5 == '3' & v529  == '4' ~ 0,
          v5 == '3' & v529  == '5' ~ 0,
          v5 == '3' & v529  == '6' ~ 0,
          v5 == '3' & v529  == '7' ~ 0,
          
          v5 == '4' & v529  == '1' ~ 1,
          v5 == '4' & v529  == '2' ~ 1,
          v5 == '4' & v529  == '3' ~ 1,
          v5 == '4' & v529  == '4' ~ 0,
          v5 == '4' & v529  == '5' ~ 0,
          v5 == '4' & v529  == '6' ~ 0,
          v5 == '4' & v529  == '7' ~ 0,
          
          v5 == '5' & v529  == '1' ~ 1,
          v5 == '5' & v529  == '2' ~ 0,
          v5 == '5' & v529  == '3' ~ 0,
          v5 == '5' & v529  == '4' ~ 0,
          
          v5 == '6' & v529  == '1' ~ 1,
          v5 == '6' & v529 == '2' ~ 1,
          v5 == '6' & v529 == '3' ~ 1,
          v5 == '6' & v529 == '4' ~ 0,
          v5 == '6' & v529 == '5' ~ 0,
          v5 == '6' & v529 == '6' ~ 0,
          
          v5 == '7' & v529 == '1' ~ 1,
          v5 == '7' & v529 == '2' ~ 1,
          v5 == '7' & v529 == '3' ~ 1,
          v5 == '7' & v529 == '4' ~ 0,
          v5 == '7' & v529 == '5' ~ 0,
          v5 == '7' & v529 == '6' ~ 0,
          v5 == '7' & v529 == '7' ~ 0,
          
          v5 == '8' & v529 == '1' ~ 1,
          v5 == '8' & v529 == '2' ~ 1,
          v5 == '8' & v529 == '3' ~ 0,
          v5 == '8' & v529 == '4' ~ 0,
          
          isocntry == 'GB-GBN' & v529 == '1' ~ 0,
          isocntry == 'GB-GBN' & v529 == '2' ~ 0,
          isocntry == 'GB-GBN' & v529 == '3' ~ 0,
          isocntry == 'GB-GBN' & v529 == '4' ~ 1,
          
          isocntry == 'GB-NIR' & v529 == '1' ~ 1,
          isocntry == 'GB-NIR' & v529 == '2' ~ 1,
          isocntry == 'GB-NIR' & v529 == '3' ~ 0,
          isocntry == 'GB-NIR' & v529 == '4' ~ 0,
          
          v5 == '10' & v529 == '1' ~ 1,
          v5 == '10' & v529 == '2' ~ 1,
          v5 == '10' & v529 == '3' ~ 0,
          v5 == '10' & v529 == '4' ~ 0,
          v5 == '10' & v529 == '5' ~ 0,
          
          v5 == '11' & v529 == '1' ~ 1,
          v5 == '11' & v529 == '2' ~ 1,
          v5 == '11' & v529 == '3' ~ 0,
          v5 == '11' & v529 == '4' ~ 0,
          v5 == '11' & v529 == '5' ~ 0,
          v5 == '11' & v529 == '6' ~ 0,
          v5 == '11' & v529 == '7' ~ 0,
          v5 == '11' & v529 == '8' ~ 0,
          
          v5 == '12' & v529 == '1' ~ 1,
          v5 == '12' & v529 == '2' ~ 1,
          v5 == '12' & v529 == '3' ~ 1,
          v5 == '12' & v529 == '4' ~ 1,
          v5 == '12' & v529 == '5' ~ 1,
          v5 == '12' & v529 == '6' ~ 1,
          v5 == '12' & v529 == '7' ~ 1,
          v5 == '12' & v529 == '8' ~ 0,
          v5 == '12' & v529 == '9' ~ 0,
          v5 == '12' & v529 == '10' ~ 0,
          v5 == '12' & v529 == '11' ~ 0,
          
          v5 == '13' & v529 == '1' ~ 1,
          v5 == '13' & v529 == '2' ~ 1,
          v5 == '13' & v529 == '3' ~ 1,
          v5 == '13' & v529 == '4' ~ 0,
          v5 == '13' & v529 == '5' ~ 0,
          v5 == '13' & v529 == '6' ~ 0,
          v5 == '13' & v529 == '7' ~ 0,
          
          v5 == '14' & v529 == '1' ~ 0,
          v5 == '14' & v529 == '2' ~ 0,
          v5 == '14' & v529 == '3' ~ 0,
          v5 == '14' & v529 == '4' ~ 0,
          v5 == '14' & v529 == '5' ~ 1,
          
          v5 == '15' & v529 == '1' ~ 0,
          v5 == '15' & v529 == '2' ~ 0,
          v5 == '15' & v529 == '3' ~ 0,
          v5 == '15' & v529 == '4' ~ 1,
          v5 == '15' & v529 == '5' ~ 1,
          
          v5 == '17' & v529 == '1' ~ 1,
          v5 == '17' & v529 == '2' ~ 0,
          v5 == '17' & v529 == '3' ~ 0,
          v5 == '17' & v529 == '4' ~ 0),
    
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: v439 [Q.85C]; QTEXT: Some people are disturbed by the opinions, customs and way of life of people different from themselves. And do you find the presence of people of another religion disturbing?; ROPTIONS: 1 = Disturbing [=1] + 2 = Not Disturbing [=0]; TARGET: Different religion; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(v439),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v439 %in% c(1) ~ 1,
            v439 %in% c(2) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: D28 [v498]; QTEXT: Whether you do or you don't follow religious practices, would you say that you are...; ROPTIONS: 1 = Religious + 2 = Not religious + 3 = An agnostic + 4 = An atheist",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v498),
            `0` = NA_real_,
            `5` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = 
          case_when(
            v498 == 1 ~ 1,
            v498 %in% c(2:4) ~ 0,
            TRUE ~ NA_real_
          )

    ) %>% 
    select(starts_with("resp_"))
```

# Clean Eurobarometer 47.2

```{r}
# clean
  clean_eurobarometer_47.2 <- 
    eurobarometer_47.2_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 47.2",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3lWZIdQ",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = dplyr::recode(
            as.character(isocntry),
            "AT" = "Austria",
            "BE" = "Belgium",
            "DE-E" = "Germany",
            "DE-W" = "Germany",
            "DK" = "Denmark",
            "ES" = "Spain",
            "FI" = "Finland",
            "FR" = "France",
            "GB-GBN" = "United Kingdom",
            "GB-NIR" = "United Kingdom",
            "GR" = "Greece",
            "IE" = "Ireland",
            "IT" = "Italy",
            "LU" = "Luxembourg",
            "NL" = "Netherlands",
            "PT" = "Portugal",
            "SE" = "Sweden",
            .default = NA_character_),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(dplyr::recode(
            as.character(v339),
            "1" = "1997-04-20",
            "2" = "1997-04-21",
            "3" = "1997-04-22",
            "4" = "1997-04-23",
            "5" = "1997-04-24",
            "6" = "1997-04-25",
            "7" = "1997-04-26",
            "8" = "1997-04-27",
            "9" = "1997-04-28",
            "10" = "1997-04-29",
            "11" = "1997-04-30",
            "12" = "1997-05-01",
            "13" = "1997-05-02",
            "14" = "1997-05-03",
            "15" = "1997-05-04",
            "16" = "1997-05-05",
            "17" = "1997-05-06",
            "18" = "1997-05-07",
            "19" = "1997-05-08",
            "20" = "1997-05-09",
            "21" = "1997-05-10",
            "22" = "1997-05-11",
            "23" = "1997-05-12",
            "24" = "1997-05-13",
            "25" = "1997-05-14",
            "26" = "1997-05-15",
            "27" = "1997-05-16",
            "28" = "1997-05-17",
            "29" = "1997-05-18",
            "30" = "1997-05-19",
            "31" = "1997-05-20",
            "32" = "1997-05-21",
            "33" = "1997-05-22",
            "34" = "1997-05-23",
            "35" = "1997-05-24",
            "36" = "1997-05-25",
            "37" = "1997-05-26",
            "38" = "1997-05-27",
            "39" = "1997-05-28",
            "40" = "1997-05-29",
            "41" = "1997-05-30",
            "42" = "1997-05-31",
            "43" = "1997-06-01",
            "44" = "1997-06-02",
            "45" = "1997-06-03",
            "46" = "1997-06-04",
            "47" = "1997-06-05",
            "48" = "1997-06-06",
            "49" = "1997-06-07",
            .default = NA_character_)),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(v338),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Jewish",
            "5" = "Muslim",
            "6" = "Buddhist",
            "7" = "Hindu",
            "8" = "Other religion",
            "9" = NA_character_,
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(v338),
    
      # respondent's age
        resp_age = as.character(v329),
    
      # respondent's education level
        resp_education_original = 
          dplyr::recode(
            as.character(v327),
            "1" = "1. Stopped by 14 years old [Primary]",
            "2" = "2. Stopped at 15 years old [Primary]",
            "3" = "3. Stopped at 16 years old [Primary]",
            "4" = "4. Stopped at 17 years old [Primary]",
            "5" = "5. Stopped at 18 years old [Primary]",
            "6" = "6. Stopped at 19 years old [Primary]",
            "7" = "7. Stopped at 20 years old [Primary]",
            "8" = "8. Stopped at 21 years old [College]",
            "9" = "9. Stopped at 22 years or older [College]",
            "10" = "10. Still studying [Primary]"),  
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = as.numeric((v328 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = 
          case_when(
          v6 == '1' & v361 == '1' ~ 1,
          v6 == '1' & v361  == '2' ~ 1,
          v6 == '1' & v361  == '3' ~ 0,
          v6 == '1' & v361  == '4' ~ 0,
          v6 == '1' & v361  == '5' ~ 0,
          
          v6 == '2' & v361  == '1' ~ 0,
          v6 == '2' & v361  == '2' ~ 0,
          v6 == '2' & v361  == '3' ~ 1,
          v6 == '2' & v361  == '4' ~ 1,
          
          v6 == '3' & v361  == '1' ~ 1,
          v6 == '3' & v361  == '2' ~ 1,
          v6 == '3' & v361  == '3' ~ 0,
          v6 == '3' & v361  == '4' ~ 0,
          v6 == '3' & v361  == '5' ~ 0,
          v6 == '3' & v361  == '6' ~ 0,
          v6 == '3' & v361  == '7' ~ 0,
          
          v6 == '4' & v361  == '1' ~ 1,
          v6 == '4' & v361  == '2' ~ 1,
          v6 == '4' & v361  == '3' ~ 1,
          v6 == '4' & v361  == '4' ~ 0,
          v6 == '4' & v361  == '5' ~ 0,
          v6 == '4' & v361  == '6' ~ 0,
          v6 == '4' & v361  == '7' ~ 0,
          
          v6 == '5' & v361  == '1' ~ 1,
          v6 == '5' & v361  == '2' ~ 0,
          v6 == '5' & v361  == '3' ~ 0,
          v6 == '5' & v361  == '4' ~ 0,
          
          v6 == '6' & v361  == '1' ~ 1,
          v6 == '6' & v361 == '2' ~ 1,
          v6 == '6' & v361 == '3' ~ 1,
          v6 == '6' & v361 == '4' ~ 0,
          v6 == '6' & v361 == '5' ~ 0,
          v6 == '6' & v361 == '6' ~ 0,
          
          v6 == '7' & v361 == '1' ~ 1,
          v6 == '7' & v361 == '2' ~ 1,
          v6 == '7' & v361 == '3' ~ 1,
          v6 == '7' & v361 == '4' ~ 0,
          v6 == '7' & v361 == '5' ~ 0,
          v6 == '7' & v361 == '6' ~ 0,
          v6 == '7' & v361 == '7' ~ 0,
          
          v6 == '8' & v361 == '1' ~ 1,
          v6 == '8' & v361 == '2' ~ 1,
          v6 == '8' & v361 == '3' ~ 0,
          v6 == '8' & v361 == '4' ~ 0,
          
          isocntry == 'GB-GBN' & v361 == '1' ~ 0,
          isocntry == 'GB-GBN' & v361 == '2' ~ 0,
          isocntry == 'GB-GBN' & v361 == '3' ~ 0,
          isocntry == 'GB-GBN' & v361 == '4' ~ 1,
          
          isocntry == 'GB-NIR' & v361 == '1' ~ 1,
          isocntry == 'GB-NIR' & v361 == '2' ~ 1,
          isocntry == 'GB-NIR' & v361 == '3' ~ 0,
          isocntry == 'GB-NIR' & v361 == '4' ~ 0,
          
          v6 == '10' & v361 == '1' ~ 1,
          v6 == '10' & v361 == '2' ~ 1,
          v6 == '10' & v361 == '3' ~ 0,
          v6 == '10' & v361 == '4' ~ 0,
          v6 == '10' & v361 == '5' ~ 0,
          
          v6 == '11' & v361 == '1' ~ 1,
          v6 == '11' & v361 == '2' ~ 1,
          v6 == '11' & v361 == '3' ~ 0,
          v6 == '11' & v361 == '4' ~ 0,
          v6 == '11' & v361 == '5' ~ 0,
          v6 == '11' & v361 == '6' ~ 0,
          v6 == '11' & v361 == '7' ~ 0,
          v6 == '11' & v361 == '8' ~ 0,
          
          v6 == '12' & v361 == '1' ~ 1,
          v6 == '12' & v361 == '2' ~ 1,
          v6 == '12' & v361 == '3' ~ 1,
          v6 == '12' & v361 == '4' ~ 1,
          v6 == '12' & v361 == '5' ~ 1,
          v6 == '12' & v361 == '6' ~ 1,
          v6 == '12' & v361 == '7' ~ 1,
          v6 == '12' & v361 == '8' ~ 0,
          v6 == '12' & v361 == '9' ~ 0,
          v6 == '12' & v361 == '10' ~ 0,
          v6 == '12' & v361 == '11' ~ 0,
          
          v6 == '13' & v361 == '1' ~ 1,
          v6 == '13' & v361 == '2' ~ 1,
          v6 == '13' & v361 == '3' ~ 1,
          v6 == '13' & v361 == '4' ~ 0,
          v6 == '13' & v361 == '5' ~ 0,
          v6 == '13' & v361 == '6' ~ 0,
          v6 == '13' & v361 == '7' ~ 0,
          
          v6 == '15' & v361 == '1' ~ 0,
          v6 == '15' & v361 == '2' ~ 0,
          v6 == '15' & v361 == '3' ~ 0,
          v6 == '15' & v361 == '4' ~ 1,
          
          v6 == '16' & v361 == '1' ~ 1,
          v6 == '16' & v361 == '2' ~ 1,
          v6 == '16' & v361 == '3' ~ 0,
          v6 == '16' & v361 == '4' ~ 0,
          v6 == '16' & v361 == '5' ~ 0,
          v6 == '16' & v361 == '6' ~ 0,
          v6 == '16' & v361 == '7' ~ 0,
          
          v6 == '17' & v361 == '1' ~ 1,
          v6 == '17' & v361 == '2' ~ 0,
          v6 == '17' & v361 == '3' ~ 0,
          v6 == '17' & v361 == '4' ~ 0),   
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: v129 [Q.17.4]; QTEXT: Some people feel uneasy when they meet people who are different from themselves, for example, people who have different appearance, behaviour, opinions, habits or way of life. Do you feel uneasy in the presence of any people in your daily life? People of another religion.; ROPTIONS: 0 = Not Mentioned [=0] + 1 = Mentioned [=1]; TARGET: Different religion; TYPE: Discomfort",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(v129),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v129 %in% c(1) ~ 1,
            v129 %in% c(0) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q16 [v125]; QTEXT: From the following situations, linked to religious beliefs, which one best describes your personal situation?; ROPTIONS: 1 = I believe and I practice + 2 = I believe, but I don't practice + 3 = I practice a religion, but I don't really believe + 4 = I belong to a spiritual group which is not a recognised religion + 5 = I would like to join a religious group, but I haven't found a suitable one yet + 6 = I am an agnostic, I don't know whether there is a God and I do not belong to a religious group + 7 = I am an atheist, I do not believe in any God",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(v125),
            `0` = NA_real_,
            `8` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (7 - resp_religiosity_original)/6

    ) %>% 
    select(starts_with("resp_"))
```

# Round 47

Note that there is a question seemingly related; however, the dataset does not sufficiently specify religion:
(Question text: "Amongst the groups of people who live in (OUR COUNTRY) and who are not of the same race, religion or culture as yourself, which one group strikes you as being particularly different from yourself and which you find sometimes disturbing?")

# Clean Round 48

Note that there is a social distance question (Question text: Some people are disturbed by the opinions, customs and way of life of people different from themselves. And do you find the presence of people of another religion disturbing?) However, there is no variable that specifies the religion of the respondent. 


# Clean Round 53

Note that there is a social distance question (Question text: Some people are disturbed by the opinions, customs and way of life of people different from themselves. And do you find the presence of people of another religion disturbing?) However, there is no variable that specifies the religion of the respondent. 

# Clean Eurobarometer 69.1

```{r}
# clean
  clean_eurobarometer_69.1 <- 
    eurobarometer_69.1_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 69.1",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3z78GwS",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(v6),
            "1" = "France",
            "2" = "Belgium",
            "3" = "Netherlands",
            "4" = "Germany", # west
            "5" = "Italy",
            "6" = "Luxembourg",
            "7" = "Denmark",
            "8" = "Ireland",
            "9" = "United Kingdom",
            "10" = "United Kingdom",
            "11" = "Greece",
            "12" = "Spain",
            "13" = "Portugal",
            "14" = "Germany", # east
            "15" = "Norway",
            "16" = "Finland",
            "17" = "Sweden",
            "18" = "Austria",
            "19" = "Cyprus",
            "20" = "Czech Republic",
            "21" = "Estonia",
            "22" = "Hungary",
            "23" = "Latvia",
            "24" = "Lithuania",
            "25" = "Malta",
            "26" = "Poland",
            "27" = "Slovakia",
            "28" = "Slovenia",
            "29" = "Bulgaria",
            "30" = "Romania",
            .default = NA_character_),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(dplyr::recode(
            as.character(v440),
            "1" = "2008-02-18",
            "2" = "2008-02-19",
            "3" = "2008-02-20",
            "4" = "2008-02-21",
            "5" = "2008-02-22",
            "6" = "2008-02-23",
            "7" = "2008-02-24",
            "8" = "2008-02-25",
            "9" = "2008-02-26",
            "10" = "2008-02-27",
            "11" = "2008-02-28",
            "12" = "2008-02-29",
            "13" = "2008-03-01",
            "14" = "2008-03-02",
            "15" = "2008-03-03",
            "16" = "2008-03-04",
            "17" = "2008-03-05",
            "18" = "2008-03-06",
            "19" = "2008-03-07",
            "20" = "2008-03-08",
            "21" = "2008-03-09",
            "22" = "2008-03-10",
            "23" = "2008-03-11",
            "24" = "2008-03-12",
            "25" = "2008-03-13",
            "26" = "2008-03-14",
            "27" = "2008-03-15",
            "28" = "2008-03-16",
            "29" = "2008-03-17",
            "30" = "2008-03-18",
            "31" = "2008-03-19",
            "32" = "2008-03-20",
            "33" = "2008-03-22",
            .default = NA_character_)),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(v428),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Christian",
            "5" = "Jewish",
            "6" = "Muslim",
            "7" = "Sikh",
            "8" = "Buddhist",
            "9" = "Hindu",
            "10" = NA_character_,
            "11" = NA_character_,
            "12" = "Other religion",
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(v428),
    
      # respondent's age
        resp_age = as.character(v390),
    
      # respondent's education level
        resp_education_original = dplyr::recode(
            as.character(v388),
            "11" = "1. No full-time education [No education]",
            "1" = "2. Stopped by 14 years old [Primary]",
            "2" = "3. Stopped at 15 years old [Primary]",
            "3" = "4. Stopped at 16 years old [Primary]",
            "4" = "5. Stopped at 17 years old [Primary]",
            "5" = "6. Stopped at 18 years old [Primary]",
            "6" = "7. Stopped at 19 years old [Primary]",
            "7" = "8. Stopped at 20 years old [Primary]",
            "8" = "9. Stopped at 21 years old [College]",
            "9" = "10.Stopped at 22 years or older [College]",
            "10" = "11. Still studying [Primary]"), 
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = as.numeric((v389 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = 
          dplyr::recode(
            as.numeric(v395),
            "1" = 1,
            "2" = 0,
            "3" = 0,
            .default = NA_real_), 
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: v110 [Q.A6.5]; QTEXT: For each of the following situations, please tell me using this scale from 1 to 10 how you would personally feel about it: Having a person with a different religion or belief than yours as a neighbour.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Different religion; TYPE: Distance, neighbor",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(v110),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            v110 %in% c(1:5) ~ 1,
            v110 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = NA,
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = NA,       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = NA

    ) %>% 
    select(starts_with("resp_"))

```

# Clean Eurobarometer 83.4

```{r}
# clean
  clean_eurobarometer_83.4 <- 
    eurobarometer_83.4_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 83.4",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3aji5XA",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(isocntry),
            "AT" = "Austria",
            "BE" = "Belgium",
            "BG" = "Bulgaria",
            "CY" = "Cyprus",
            "CZ" = "Czech Republic",
            "DE-E" = "Germany", # east
            "DE-W" = "Germany", # west
            "DK" = "Denmark",
            "EE" = "Estonia",
            "ES" = "Spain",
            "FI" = "Finland",
            "FR" = "France",
            "GB-GBN" = "United Kingdom",
            "GB-NIR" = "United Kingdom",
            "GR" = "Greece",
            "HR" = "Croatia",
            "HU" = "Hungary",
            "IE" = "Ireland",
            "IT" = "Italy",
            "LT" = "Lithuania",
            "LU" = "Luxembourg",
            "LV" = "Latvia",
            "MT" = "Malta",
            "NO" = "Norway",
            "NL" = "Netherlands",
            "PL" = "Poland",
            "PT" = "Portugal",
            "RO" = "Romania",
            "SE" = "Sweden",
            "SI" = "Slovenia",
            "SK" = "Slovakia",
            .default = NA_character_),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = as.Date(dplyr::recode(
            as.character(p1),
            "1" = "2015-05-30",
            "2" = "2015-05-31",
            "3" = "2015-06-01",
            "4" = "2015-06-02",
            "5" = "2015-06-03",
            "6" = "2015-06-04",
            "7" = "2015-06-05",
            "8" = "2015-06-06",
            "9" = "2015-06-07",
            "10" = "2015-06-08",
            .default = NA_character_)),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(sd3),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Christian",
            "5" = "Jewish",
            "6" = "Muslim",
            "7" = "Sikh",
            "8" = "Buddhist",
            "9" = "Hindu",
            "10" = NA_character_,
            "11" = NA_character_,
            "12" = "Other religion",
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(sd3),
    
      # respondent's age
        resp_age = as.character(d11),
    
      # respondent's education level
        resp_education_original = # missingness spread evenly across countries
          dplyr::recode(
            as.character(d8r1),
            "11" = "1. No full-time education [No education]",
            "1" = "2. Stopped by 14 years [Primary]",
            "2" = "3. Stopped at 15 years [Primary]",
            "3" = "4. Stopped at 16 years [Primary]",
            "4" = "5. Stopped at 17 years [Primary]",
            "5" = "6. Stopped at 18 years [Primary]",
            "6" = "7. Stopped at 19 years [Primary]",
            "7" = "8. Stopped at 20 years [Primary]",
            "8" = "9. Stopped at 21 years [College]",
            "9" = "10. Stopped at 22 years or older [College]",
            "10" = "11. Still studying [Primary]"), 
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = as.numeric((d10 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          dplyr::recode(
            as.numeric(d25),
            "1" = 1,
            "2" = 0,
            "3" = 0,
            .default = NA_real_), 
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: qc13.5 [QC13.5]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Jewish person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Jewish; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(qc13_5),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            qc13_5 %in% c(1:5) ~ 1,
            qc13_5 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: qc13.6 [QC13.6]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Muslim person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Muslim; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_2_original = as.character(qc13_6),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            qc13_6 %in% c(1:5) ~ 1,
            qc13_6 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: qc13.7 [QC13.7]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Buddhist person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Buddhist; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_3_original = as.character(qc13_7),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            qc13_7 %in% c(1:5) ~ 1,
            qc13_7 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: qc13.8 [QC13.8]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Christian person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Christian; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_4_original = as.character(qc13_8),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            qc13_8 %in% c(1:5) ~ 1,
            qc13_8 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: qc14.5 [QC14.5]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Jewish person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Jewish; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_5_original = as.character(qc14_5),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            qc14_5 %in% c(1:5) ~ 1,
            qc14_5 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: qc14.6 [QC14.6]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Muslim person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Muslim; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_6_original = as.character(qc14_6),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            qc14_6 %in% c(1:5) ~ 1,
            qc14_6 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 7 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_7_qinfo = "NUM: qc14.7 [QC14.7]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Buddhist person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Buddhist; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_7_original = as.character(qc14_7),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_7_bin_recode = 
          case_when(
            qc14_7 %in% c(1:5) ~ 1,
            qc14_7 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 8 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_8_qinfo = "NUM: qc14_8 [QC14_8]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Christian person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Christian; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_8_original = as.character(qc14_8),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_8_bin_recode = 
          case_when(
            qc14_8 %in% c(1:5) ~ 1,
            qc14_8 %in% c(6:11) ~ 0,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = NA,
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = NA,       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = NA

    ) %>% 
    select(starts_with("resp_"))
```

# Clean Eurobarometer 91.4

```{r}
# clean
  clean_eurobarometer_91.4 <- 
    eurobarometer_91.4_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Eurobarometer",
        
      # round number (character vector, title case)  
        resp_round = "Round 91.4",
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3zbIH7z",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(isocntry),
            "AT" = "Austria",
            "BE" = "Belgium",
            "BG" = "Bulgaria",
            "CY" = "Cyprus",
            "CZ" = "Czech Republic",
            "DE-E" = "Germany", # east
            "DE-W" = "Germany", # west
            "DK" = "Denmark",
            "EE" = "Estonia",
            "ES" = "Spain",
            "FI" = "Finland",
            "FR" = "France",
            "GB" = "United Kingdom",
            "GR" = "Greece",
            "HR" = "Croatia",
            "HU" = "Hungary",
            "IE" = "Ireland",
            "IT" = "Italy",
            "LT" = "Lithuania",
            "LU" = "Luxembourg",
            "LV" = "Latvia",
            "MT" = "Malta",
            "NO" = "Norway",
            "NL" = "Netherlands",
            "PL" = "Poland",
            "PT" = "Portugal",
            "RO" = "Romania",
            "SE" = "Sweden",
            "SI" = "Slovenia",
            "SK" = "Slovakia",
            .default = NA_character_),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = 
          as.Date(dplyr::recode(
            as.character(p1),
            "1" = "2019-05-09",
            "2" = "2019-05-10",
            "3" = "2019-05-11",
            "4" = "2019-05-12",
            "5" = "2019-05-13",
            "6" = "2019-05-14",
            "7" = "2019-05-15",
            "8" = "2019-05-16",
            "9" = "2019-05-17",
            "10" = "2019-05-18",
            "11" = "2019-05-19",
            "12" = "2019-05-20",
            "13" = "2019-05-21",
            "14" = "2019-05-22",
            "15" = "2019-05-23",
            "16" = "2019-05-24",
            "17" = "2019-05-25",
            .default = NA_character_)),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          dplyr::recode(
            as.character(sd3),
            "1" = "Christian",
            "2" = "Christian",
            "3" = "Christian",
            "4" = "Christian",
            "5" = "Jewish",
            "6" = "Muslim",
            "7" = "Muslim",
            "8" = "Muslim",
            "9" = "Sikh",
            "10" = "Buddhist",
            "11" = "Hindu",
            "12" = NA_character_,
            "13" = NA_character_,
            "14" = "Other religion",
            .default = NA_character_),

      # respondent's denomination (character vector that corresponds to master list)
        resp_denomination = to_character(sd3),
    
      # respondent's age
        resp_age = as.character(d11),
    
      # respondent's education level
        resp_education_original = 
          dplyr::recode(
            as.character(d8r1),
            "11" = "1. No full-time education [No education]",
            "1" = "2. Stopped by 14 years [Primary]",
            "2" = "3. Stopped at 15 years [Primary]",
            "3" = "4. Stopped at 16 years [Primary]",
            "4" = "5. Stopped at 17 years [Primary]",
            "5" = "6. Stopped at 18 years [Primary]",
            "6" = "7. Stopped at 19 years [Primary]",
            "7" = "8. Stopped at 20 years [Primary]",
            "8" = "9. Stopped at 21 years [College]",
            "9" = "10. Stopped at 22 years or older [College]",
            "10" = "11. Still studying [Primary]"), 
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = as.numeric((d10 == 2)*1),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = 
          dplyr::recode(
            as.numeric(d25),
            "1" = 1,
            "2" = 0,
            "3" = 0,
            .default = NA_real_), 
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: qc12.5 [QC12.5]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Jewish person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Jewish; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_1_original = as.character(qc12_5),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            qc12_5 %in% c(1:5) ~ 1,
            qc12_5 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: qc12.6 [QC12.6]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? Muslim person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Muslim; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_2_original = as.character(qc12_6),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            qc12_6 %in% c(1:5) ~ 1,
            qc12_6 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),
    
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: qc12.7 [QC12.7]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Buddhist person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + 6-10 = Comfortable [=0]; TARGET: Buddhist; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_3_original = as.character(qc12_7),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            qc12_7 %in% c(1:5) ~ 1,
            qc12_7 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 4 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_4_qinfo = "NUM: qc12.8 [QC12.8]; QTEXT: Regardless of whether you are actually working or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your colleagues at work belonged to each of the following groups? A Christian person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Christian; TYPE: Distance, work",
      
      # original response (as character vector)
        resp_soc_dist_4_original = as.character(qc12_8),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_4_bin_recode = 
          case_when(
            qc12_8 %in% c(1:5) ~ 1,
            qc12_8 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 5 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_5_qinfo = "NUM: qc13.5 [QC13.5]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Jewish person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Jewish; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_5_original = as.character(qc13_5),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_5_bin_recode = 
          case_when(
            qc13_5 %in% c(1:5) ~ 1,
            qc13_5 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 6 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_6_qinfo = "NUM: qc13.6 [QC13.6]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Muslim person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Muslim; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_6_original = as.character(qc13_6),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_6_bin_recode = 
          case_when(
            qc13_6 %in% c(1:5) ~ 1,
            qc13_6 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),
    
    
    #########################  
    ### SOCIAL DISTANCE 7 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_7_qinfo = "NUM: qc13.7 [QC13.7]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Buddhist person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Buddhist; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_7_original = as.character(qc13_7),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_7_bin_recode = 
          case_when(
            qc13_7 %in% c(1:5) ~ 1,
            qc13_7 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 8 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_8_qinfo = "NUM qc13.8 [QC13.8]; QTEXT: Regardless of whether you have children or not, please tell me, using a scale from 1 to 10, how comfortable you would feel if one of your children was in a love relationship with a person from each of the following groups? A Christian person.; ROPTIONS: 1(/2/3/4/5) = Uncomfortable [=1] + (6/7/8/9)10 = Comfortable [=0]; TARGET: Christian; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_8_original = as.character(qc13_8),   

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_8_bin_recode = 
          case_when(
            qc13_8 %in% c(1:5) ~ 1,
            qc13_8 %in% c(6:11) ~ 0, # 11 means indifferent
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA,

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = NA,
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = NA,       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = NA

    ) %>% 
    select(starts_with("resp_"))
```

# Stack dataframes

```{r}
# stack
  stacked <- 
    clean_eurobarometer_30 %>% 
    bind_rows(clean_eurobarometer_37) %>% 
    bind_rows(clean_eurobarometer_39) %>%
    bind_rows(clean_eurobarometer_42) %>% 
    bind_rows(clean_eurobarometer_47.2) %>% 
    bind_rows(clean_eurobarometer_69.1) %>% 
    bind_rows(clean_eurobarometer_83.4) %>% 
    bind_rows(clean_eurobarometer_91.4)
```

# Save data

```{r}
  saveRDS(stacked, "../cleaned-data/y-23-multi-eurobarometer.rds")
```
